package org.example.ding.config;

import org.example.ding.service.DataInitializationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * 数据初始化配置
 * 在应用启动时自动初始化基础数据
 */
@Configuration
public class DataInitializationConfig {

    @Autowired
    private DataInitializationService dataInitializationService;

    /**
     * 应用启动时执行数据初始化
     */
    @Bean
    @Order(1)
    public CommandLineRunner initializeData() {
        return args -> {
            try {
                System.out.println("开始初始化基础数据...");
                dataInitializationService.initializeAllData();

                // 检查菜品数据
                System.out.println("基础数据初始化完成！");
                System.out.println("检查菜品数据...");
                dataInitializationService.checkDishData();
            } catch (Exception e) {
                System.err.println("数据初始化失败: " + e.getMessage());
                e.printStackTrace();
            }
        };
    }
}
